package iofile;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStream_ {

    public static void main(String[] args) {

    }

    public void writeFile(){

        //创建FileOutputStream对象
        String filePath = "";
        FileOutputStream fileOutputStream = null;
        try {
            //FileOutputStream(String path)创建的输出流，默认会覆盖原有内容
            //FileOutputStream(String path,Boolean append)创建的输出流，传参为true时，会在原文件的末尾追加内容
            fileOutputStream = new FileOutputStream(filePath);

            //写入一个字节，参数为int，char可以自动转换为int
            //文件不存在时，会自动创建
            fileOutputStream.write('a');
            //写入一个字符串，getBytes()方法可以将字符串转换成byte []
            String str = "aaaaaaa";
            fileOutputStream.write(str.getBytes());
            //将byte []从off位开始的len个字节写入
            fileOutputStream.write(str.getBytes(),2,3);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

    }
}
